package cn.stylefeng.guns.modular.web.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.web.entity.CumulaDay;
import cn.stylefeng.guns.modular.web.entity.MagicBoxType;
import cn.stylefeng.guns.modular.web.service.ICumulaDayService;
import cn.stylefeng.guns.modular.web.service.IMagicBoxTypeService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：魔盒类型表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-20
 */
@Controller
@RequestMapping("/web/magicBoxType")
public class MagicBoxTypeController extends BaseController {

    private String PREFIX = "/modular/web/magicBoxType/";

    @Autowired
    private IMagicBoxTypeService magicBoxTypeService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "magicBoxType.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = magicBoxTypeService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String magicBoxTypeId, Model model) {
        model.addAttribute("magicBoxTypeId", magicBoxTypeId);
        return PREFIX + "magicBoxType_add.html";
    }

    /**
     * 保存
     * @param magicBoxType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(MagicBoxType magicBoxType) {
        if (ToolUtil.isEmpty(magicBoxType.getId())) {
            //判断类型名称是否存在
            MagicBoxType bt = magicBoxTypeService.getOne(new QueryWrapper<MagicBoxType>()
                    .eq("name", magicBoxType.getName())
                    .eq("del_flag", 0));
            if(ToolUtil.isNotEmpty(bt)){
                throw new ServiceException(BizExceptionEnum.EXISTED_BOX_TYPE);
            }

            magicBoxType.preInsert();
            magicBoxTypeService.save(magicBoxType);
        }else{
            magicBoxType.preUpdate();
            magicBoxTypeService.updateById(magicBoxType);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param magicBoxTypeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{magicBoxTypeId}")
    public Object getInfoById(@PathVariable("magicBoxTypeId") String magicBoxTypeId){
        MagicBoxType magicBoxType = magicBoxTypeService.getById(magicBoxTypeId);
        return magicBoxType;
    }

    /**
     * 删除
     * @param magicBoxTypeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "magicBoxTypeId") String magicBoxTypeId){
        if(ToolUtil.isEmpty(magicBoxTypeId)){
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        MagicBoxType magicBoxType = magicBoxTypeService.getById(magicBoxTypeId);
        magicBoxType.preUpdate();
        magicBoxType.setDelFlag(1);
        magicBoxTypeService.updateById(magicBoxType);
        return SUCCESS_TIP;
    }

}
